package com.plan.dao.service.card.impl;

import com.plan.dao.mapper.card.DayPlanMapper;
import com.plan.dao.mapper.card.DayPlanStatMapper;
import com.plan.dao.mapper.card.TaskRepositoryMapper;
import com.plan.dao.mapper.card.TeamMapper;
import com.plan.dao.service.card.TaskRepositoryDaoService;
import com.plan.entity.card.DayPlan;
import com.plan.entity.card.DayPlanStat;
import com.plan.entity.card.Task;
import com.plan.entity.card.Team;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TaskRepositoryDaoServiceImpl implements TaskRepositoryDaoService {
    @Resource
    private TeamMapper teamMapper;
    @Resource
    private DayPlanMapper dayPlanMapper;
    @Resource
    private DayPlanStatMapper dayPlanStatMapper;
    @Resource
    private TaskRepositoryMapper taskRepositoryMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(Task task, Team team) {
        teamMapper.update(team);
        taskRepositoryMapper.insert(task);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id, Team team) {
        teamMapper.update(team);
        taskRepositoryMapper.delete(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void copyRepositoryTask(DayPlanStat dayPlanStat, List<DayPlan> dayPlanList) {
        if (dayPlanStat != null) {
            if (dayPlanStat.getId() == null) {
                dayPlanStatMapper.insert(dayPlanStat);
            } else {
                dayPlanStatMapper.update(dayPlanStat);
            }
        }
        if (!CollectionUtils.isEmpty(dayPlanList)) {
            dayPlanMapper.batchInsert(dayPlanList);
        }
    }

}
